/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Commons {

  public static String getHeader(HttpServletRequest request, HttpServletResponse response, 
          String title, int depth) 
    throws IOException {
    request.setCharacterEncoding("UTF8");
    response.setContentType("text/html; charset=utf-8");
    response.setHeader("progma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    
    StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
    sb.append("\"http://www.w3.org/TR/html4/loose.dtd\">\n");
    sb.append("<html>\n");
    sb.append("<head>\n");
    sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
    sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\"/>\n");
    sb.append("<meta http-equiv=\"Cache-Control\" content=\"no-cache\"/>\n");
    sb.append("<title>"+title+"</title>\n");
    sb.append("<link rel=\"stylesheet\" href=\"../css/common.css\" type=\"text/css\" />\n");
    sb.append("<link rel=\"stylesheet\" href=\"../css/external/jquery-tipsy.css\" type=\"text/css\" />\n");
    sb.append("<link rel=\"stylesheet\" href=\"../css/datatable.css\" type=\"text/css\" />\n");
    sb.append("<script type=\"text/javascript\" src=\"../js/external/jquery.js\"></script>\n");
    sb.append("<script type=\"text/javascript\"><!-- jQuery.noConflict(); //--></script>\n");
    sb.append("<script type=\"text/javascript\" src=\"../js/external/jquery-tipsy.js\"></script>\n");
    sb.append("<script type=\"text/javascript\" src=\"../js/external/prototype.js\"></script>\n");
    sb.append("<script type=\"text/javascript\" src=\"../js/resources.js.jsp\"></script>\n");
    sb.append("<script type=\"text/javascript\" src=\"../js/shared.js\"></script>\n");
    sb.append("</head>\n");
    sb.append("<body onLoad=\"");
    if (depth==0) {
      sb.append("loadHeader( lang );");
    } else if (depth==1) {
      sb.append("loadHeaderSub( lang );");
    } if (depth==2) {
      sb.append("loadHeaderSubSub( lang );");
    }
    sb.append("\">\n");
    sb.append("\n");
    sb.append("<!-- HEADER -->\n");
    sb.append("<div id=\"header\"></div>\n");
    sb.append("\n");
    sb.append("<h2>"+title+"</h2>\n");
    sb.append("<br>\n");
    return sb.toString();
  }
  
  public static String getFooter(HttpServletRequest request, HttpServletResponse response) 
    throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("<!-- FOOTER -->\n");
    sb.append("<div id=\"footer\"></div>\n");
    sb.append("<div id=\"msg\"></div>\n");
    sb.append("\n");
    sb.append("</body>\n");
    sb.append("</html>\n");
    return sb.toString();
  }
  
  public static void printHeader(HttpServletRequest request, HttpServletResponse response,String title,int depth) 
  throws IOException {
    response.getOutputStream().println(getHeader(request,response,title,depth));
  }
  
  public static void printFooter(HttpServletRequest request, HttpServletResponse response) 
  throws IOException {
    response.getOutputStream().println(getFooter(request,response));
  }
    
}
